<?php $this->JQuery()->uienable() ?>
<?php $this->JQuery()->javascriptCaptureStart() ?>

$j(document).ready(function() {
    $j('#form_add').submit(function(e) {
        $j.post(
            '<?php echo $this->geturl('add','homepage', null, 'admin1')?>',
            {parent:$j('#parent').attr('value'), val:$j('#name').attr('value')}
        );
        return false;
    });

    $j('#form_edit').submit(function(e) {
        $j.post(
            '<?php echo $this->geturl('edit','homepage', null, 'admin1')?>',
            $j('#form_edit').serialize()
        );
        return false;
    });

    var fixHelper = function(e, ui) {
        ui.children().each(function() {
            $j(this).width($j(this).width());
        });
        return ui;
    };

    $j("#sortable tbody").sortable({
        helper: fixHelper,
        stop: function (event, ui) {
            var data = [];
            var pos = <?php echo $this->page>1 ? ($this->page - 1) * $this->per_page :0 ;?>;
            $j("#sortable tbody tr").each(function() {
               data.push({'id':$j(this).attr('rel'), pos:pos++});
            });
            $j.post(
                '<?php echo $this->geturl('position','homepage', null, 'admin1')?>',
                {data:data}
            );
        }
    }).disableSelection();

    $j('.homepage').click(function() {
        var ele = $j(this);
        if (ele.html() == '0') {
            var act = '1';
        } else {
            var act ='';
        }
        $j.get(
            '<?php echo $this->geturl('homepage','homepage', null, 'admin1')?>',
            {id: $j(this).attr('rel'), act:act},
            function(data) {
                ele.html(data);
            }
        );
        return false;
    });
});
<?php $this->JQuery()->javascriptCaptureEnd() ?>

<?php $this->headStyle()->captureStart()?>
    #content td { padding:5px}
    #content table { border:1px #aaa solid; padding:5px; width:100%}
    #content th {font-weight:bold;}
    #content .page a {padding:2px;}
<?php $this->headStyle()->captureEnd()?>

<form id="form_add">
    <?php if(!empty($this->category)):?>
        <input type="hidden" name="parent" id="parent" value="<?php echo $this->category->_id?>">
    <?php endif;?>
    <input type="text" name="name" id="name" value="">
    <input type="submit" value="Add">
</form>
<br>
<a href="<?php echo $this->geturl('list', 'homepage', null, 'admin1')?>">Home</a> >>
<?php if(!empty($this->category)):?>
    <?php foreach($this->category->getParentTree() as $cat):?>
        <a href="<?php echo $this->geturl('list', 'homepage', null, 'admin1', array('id'=> $cat->_id))?>"><?php echo $cat->name?></a> >>
   <?php endforeach;?>
    <br>
    <form id="form_edit">
        <input type="hidden" name="edit_id" id="edit_id" value="<?php echo $this->category->_id?>">
        <table border="1">
            <tr>
                <td colspan="2">
                    <?php if(empty($this->category->homepage)):?>
                        <a href="<?php echo $this->geturl('homepage', 'homepage', null, 'admin1', array('id'=> $this->category->_id, 'act' => 1, 'back' => $_GET['id']))?>">Display on homepage</a>
                    <?php else: ?>
                        <a href="<?php echo $this->geturl('homepage', 'homepage', null, 'admin1', array('id'=> $this->category->_id, 'act' => 0, 'back' => $_GET['id']))?>">Hide from homepage</a>
                    <?php endif;?>
                </td>
            </tr>
            <tr>
                <th>Name</th>
                <td><input type="text" name="name" id="name_edit" value="<?php echo $this->category->name?>"></td>
            </tr>
            <tr>
                <th>Special handle (id)</th>
                <td><input type="text" name="handle" id="handle_edit" value="<?php echo $this->category->id?>"></td>
            </tr>
            <tr>
                <th>Status</th>
                <td><?php echo $this->category->status?></td>
            </tr>
            <tr>
                <th>Display on homepage</th>
                <td>
                    <input type="radio" name="homepage" id="homepage_edit_off" value="0" <?php echo $this->category->homepage!=1 ? 'checked' : ''?>><label for="homepage_edit">Off</label>
                    <input type="radio" name="homepage" id="homepage_edit_on" value="1" <?php echo $this->category->homepage==1 ? 'checked' : ''?>><label for="homepage_edit">On</label>
                </td>
            </tr>
        </table>
        <input type="submit" value="Validate">
    </form>
<?php else:?>
    <table border="1" id="sortable">
    <?php
        $_GET['id'] = !empty($_GET['id']) ? $_GET['id']: '';
        $get = $_GET; unset($get['page']);
        $pagination = $this->pagination($this->listing_number, $this->page, $this->per_page, 4, $this->geturl('list', 'homepage', null, 'admin1', $get));
    ?>
    <?php if($this->listing_number == 0):?>
        No Categories are defined
    <?php else:?>
        <thead>
            <tr>
                <th>

                </th>
                <th>
                    Name
                </th>
                <!--th>
                    <a href="<?php echo $this->geturl('list', 'homepage', null, 'admin1', array('sort' => 'recent', 'page' => $this->page))?>">Creation date</a>
                </th-->
                <th>
                    <a href="<?php echo $this->geturl('list', 'homepage', null, 'admin1', array('sort' => 'status', 'page' => $this->page))?>">Status</a>
                </th>
                <th>Homepage</th>
            </tr>
        </thead>
        <tbody>
        <?php foreach ($this->list as $item):?>
            <tr class="ui-state-default" rel="<?php echo $item->_id?>">
                <td>
                    <?php if($item->status == Model_Category::STATUS_DELETED):?>
                        <a href="<?php echo $this->geturl('activate', 'homepage', null, 'admin1', array('id'=> $item->_id, 'back' => $_GET['id']))?>">Activate</a>
                    <?php else: ?>
                        <a href="<?php echo $this->geturl('delete', 'homepage', null, 'admin1', array('id'=> $item->_id, 'back' => $_GET['id']))?>">Delete</a>
                    <?php endif;?>
                </td>
                <td>
                    <a href="<?php echo $this->geturl('list', 'homepage', null, 'admin1', array('id'=> $item->_id, 'back' => $_GET['id']))?>">
                        <?php echo htmlentities((string)empty($item->nav_name) ? $item->name : $item->nav_name, ENT_COMPAT, 'utf-8'); ?>
                    </a>
                </td>
                <!--td>
                    <?php echo date('Y-m-d h:i:s', $item->created_at->sec)?><?php //echo date ('Y-m-d H:i', strtotime($item->created_at))?>
                </td-->
                <td>
                    <?php echo $item->status?>
                </td>
                <td>
                    <span rel="<?php echo $item->_id?>" class="homepage"><?php echo (int)$item->homepage?></span>
                </td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    <?php endif;?>

    </table>
    <?php echo $pagination;?>
<?php endif;?>